package cn.glinks.liuhe.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.api.jsonbean.PersonalDetailsInfo;
import cn.glinks.liuhe.app.AppApplication;
import cn.glinks.liuhe.base.BaseActivity;
import cn.glinks.liuhe.utils.ActTransAnimUtils;


/**
 * Des:个人详细页面
 *
 * @author MarsBlade of Cyh
 * @created 2017-03-21 14:43
 */
public class PersonalDetailsActivity extends BaseActivity {

    @BindView(R.id.tv_comment)
    TextView mCommentText;

    @BindView(R.id.tv_advice)
    TextView mAdviceText;

    @BindView(R.id.tv_proposal)
    TextView mProposalText;

    /*---------------个人资料->START---------------*/
    @BindView(R.id.et_name)             //姓名
    EditText mNameEdit;

    @BindView(R.id.et_race)             //民族
    EditText mRaceEdit;

    @BindView(R.id.et_marital)          //婚姻
    EditText mMaritalEdit;

    @BindView(R.id.et_id)               //身份证号
    EditText mIdEdit;

    @BindView(R.id.et_phone)            //手机号
    EditText mPhoneEdit;

    @BindView(R.id.et_education)        //学历
    EditText mEducationEdit;

    @BindView(R.id.et_graduate_time)    //毕业时间
    EditText mGraduateTimeEdit;

    @BindView(R.id.et_graduate_school)  //毕业院校
    EditText mGraduateSchoolEdit;

    @BindView(R.id.et_major)            //所学专业
    EditText mMajorEdit;

    @BindView(R.id.et_life_experience)  //成长经历
    EditText mLifeExperienceEdit;
    /*---------------个人资料->END-----------------*/

    /*---------------爱好和性格->START---------------*/
    @BindView(R.id.et_hobby)            // 愛好
    EditText mHobbyEdit;

    @BindView(R.id.et_sport)            // 运动
    EditText mSportEdit;

    @BindView(R.id.et_travel)           // 旅游
    EditText mTravelEdit;

    @BindView(R.id.et_intercourse)      // 交往
    EditText mIntercourseEdit;

    @BindView(R.id.et_living)           // 居住
    EditText mLivingEdit;

    @BindView(R.id.et_character)        // 性格
    EditText mCharacterEdit;

    private String mHobbyStr = "";
    private String mSportStr = "";
    private String mTravelStr = "";
    private String mIntercourseStr = "";
    private String mLivingStr = "";
    private String mCharacterStr = "";
    /*---------------爱好和性格->END-----------------*/

    // 变量
    private PersonalDetailsInfo mInfo = null;// 当前个人缓存信息

    @Override
    public int getLayoutId() {
        return R.layout.activity_personal_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        // 设置为透明状态栏
        SetTranslanteBar();
        // 获取测评结果缓存
        mInfo = ((AppApplication) getApplicationContext()).getPersonalDetailsInfo();
        if (mInfo != null) {
            showAllData(mInfo);
        }
    }

    /*--------------------------------------------------------------------------------
     *                          基本封装接口
     ---------------------------------------------------------------------------------*/
    /**
     * Des:显示全部数据
     * @param info
     */
    private void showAllData(PersonalDetailsInfo info) {
        mCommentText.setText(mInfo.getComment().getComment());
        mAdviceText.setText(mInfo.getComment().getAdvice());
        mProposalText.setText(mInfo.getCommentConsultant());

        mNameEdit.setText(mInfo.getPersonalInfo().getName());
        mRaceEdit.setText(mInfo.getPersonalInfo().getNationality());
        mMaritalEdit.setText(mInfo.getPersonalInfo().getMarried());
        mIdEdit.setText(mInfo.getPersonalInfo().getIdNo());
        mPhoneEdit.setText(mInfo.getPersonalInfo().getPhoneNo());
        mEducationEdit.setText(mInfo.getPersonalInfo().getEducation());
        mGraduateTimeEdit.setText(mInfo.getPersonalInfo().getGraduateTime());
        mGraduateSchoolEdit.setText(mInfo.getPersonalInfo().getCollege());
        mMajorEdit.setText(mInfo.getPersonalInfo().getSpecialty());
        mLifeExperienceEdit.setText(mInfo.getPersonalInfo().getGrowthExp());

        // 获取爱好和性格数据
        if (mInfo.getPersonalInterest().getHobby() != null) {
            mHobbyStr = mInfo.getPersonalInterest().getHobby().replace("|", "、");
        }
        if (mInfo.getPersonalInterest().getSport() != null) {
            mSportStr = mInfo.getPersonalInterest().getSport().replace("|", "、");
        }
        if (mInfo.getPersonalInterest().getTravel() != null) {
            mTravelStr = mInfo.getPersonalInterest().getTravel().replace("|", "、");
        }
        if (mInfo.getPersonalInterest().getIntercourse() != null) {
            mIntercourseStr = mInfo.getPersonalInterest().getIntercourse().replace("|", "、");
        }
        if (mInfo.getPersonalInterest().getLiving() != null) {
            mLivingStr = mInfo.getPersonalInterest().getLiving().replace("|", "、");
        }
        if (mInfo.getPersonalInterest().getCharacter()!= null) {
            mCharacterStr = mInfo.getPersonalInterest().getCharacter().replace("|", "、");
        }
        showInterestData();
    }

    /**
     * Des:加载爱好子项目选中数据
     * @param editText 要加载的EditText对象
     * @param data 数据
     * @return
     */
    private void loadInterestCheckedData(final EditText editText, final String data) {
        editText.setText(data);
        editText.post(new Runnable() {
            @Override
            public void run() {
                if (editText.getLineCount() > 1) {
                    editText.setGravity(Gravity.START);
                } else {
                    editText.setGravity(Gravity.END);
                }
            }
        });
    }

    /**
     * Des:初始化爱好子项目显示数据
     */
    private void showInterestData() {
        loadInterestCheckedData(mHobbyEdit, mHobbyStr);
        loadInterestCheckedData(mSportEdit, mSportStr);
        loadInterestCheckedData(mTravelEdit, mTravelStr);
        loadInterestCheckedData(mIntercourseEdit, mIntercourseStr);
        loadInterestCheckedData(mLivingEdit, mLivingStr);
        loadInterestCheckedData(mCharacterEdit, mCharacterStr);
    }

    /*--------------------------------------------------------------------------------
     *                          点击事件接口实现
     ---------------------------------------------------------------------------------*/
    // 退出测评
    @OnClick(R.id.iv_back)
    public void clickExitApp() {
        // 返回
        ActTransAnimUtils.finishActionSlideLeftToRight(PersonalDetailsActivity.this);
    }

    /*--------------------------------------------------------------------------------
     *                          系统键处理
     ---------------------------------------------------------------------------------*/

    /**
     * 监听主页键,返回键
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 返回
            ActTransAnimUtils.finishActionSlideLeftToRight(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
